Skip to content

Conversation

@spotandjake
Copy link
Contributor

This pr adds BinaryenHasMemorySegment(module, name) to the c api and module.hasMemorySegment(name) to the js api.

While adding a test to the grain compiler to check the size of a generated memory segment, I noticed that if the segment didn't exist, any operations on that segment would throw. As we are working from OCaml, we have no way to catch this fatal C++ exception related grain pr. As far as I am aware there is no other existing way to check if a segment exists.

This function allows a user to check for the existance of a segment before trying to perform operations on it.

Related Binaryen.ml Issue
Related Grain issue

This pr adds `BinaryenHasMemorySegment(module, name)` to the c api and `module.hasMemorySegment(name)` to the js api.
@spotandjake spotandjake force-pushed the spotandjake/hasMemorySegment branch from b7f5068 to cd887c1 Compare February 11, 2026 00:48
@kripken
Copy link
Member

kripken commented Feb 12, 2026

The current BinaryenGetFunction etc. return null if the thing is not present. Can we use the same pattern here, adding BinaryenGetDataSegment? Otherwise this has* seems like it would be a novel pattern in the C API.

@spotandjake
Copy link
Contributor Author

The current BinaryenGetFunction etc. return null if the thing is not present. Can we use the same pattern here, adding BinaryenGetDataSegment? Otherwise this has* seems like it would be a novel pattern in the C API.

I certainly could implement a BinaryenGetDataSegment however I wanted to note that currently there is no concept of MemorySegmentRef. The behaviour of BinarenGetFunction seems fundamentally different as it returns FunctionRef, I took the Has syntax from inspiration from BinaryenHasMemory, BinaryenHasPassToSkip, BinaryenFunctionHasLocalName though i'm not sure any of those examples are exaclty the same either.

The options I see going forward here would be:

  • Make all the memory segment opertaions return NULL instead of throwing if the segment doesn't exist (this doesn't feel super explcit to me so i'm not the biggest fan of this behaviour.
  • Add a BinaryenGetDataSegment function, add a MemorySegmentRef, deprecate the existing name basedMemorySegment operations and implement reference based operations
  • The approach I currently took.

I am happy todo any one of those, I just wanted to make sure the pr scope was clear before implementing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants